<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<!DOCTYPE html>
<html>
    <jsp:include page="../incHead.jsp">
        <jsp:param name="title" value="mushroom.title" />
    </jsp:include>
    <body>        
        <jsp:include page="../incHeader.jsp"/>
        <div id="container">
            <jsp:include page="../incMenu.jsp"/>
            <div id="content">
                <div class="divOfForms">
                    <div id="title_subsection">
                        <h3><fmt:message key="mushroom.addMushroom"/></h3>
                    </div>                
                    <form:form action="addMushroom" method="POST">
                        <table class="tableOfForms">
                            <tr>
                                <td class="outputInTable"><form:label path="name"><fmt:message key="mushroom.name"/></form:label></td>
                                <td><form:input path="name" /></td>
                                <td><form:errors path="name" cssClass="error"/></td>
                            </tr>
                            <tr>
                                <td class="outputInTable"><form:label path="type"><fmt:message key="mushroom.type"/></form:label></td>
                                <td><form:select path="type">
                                <option value=""><fmt:message key="general.prompt"/></option>
                                <c:forEach var="type" items="${items}">
                                    <c:if test="${type == command.type}">
                                        <option value="${type}" selected="selected"><fmt:message key="${type}"/></option>
                                    </c:if>
                                    <c:if test="${type != command.type}">
                                        <option value="${type}"><fmt:message key="${type}"/></option>
                                    </c:if>
                                </c:forEach>
                            </form:select>
                            </td>
                            <td><form:errors class="error" path="type" cssClass="error"/></td>                   
                            </tr>
                            <tr>
                                <td class="outputInTable"><form:label path="occurenceFrom"><fmt:message key="mushroom.occurenceFrom"/></form:label></td>
                                    <td>
                                    <form:select path="occurenceFrom">
                                <option value=""><fmt:message key="general.prompt"/></option>
                                <c:forEach var="month" items="${months}">
                                    <c:if test="${month == command.occurenceFrom}">
                                        <option value="${month}" selected="selected"><fmt:message key="${month}"/></option>
                                    </c:if>
                                    <c:if test="${month != command.occurenceFrom}">
                                        <option value="${month}"><fmt:message key="${month}"/></option>
                                    </c:if>
                                </c:forEach>
                            </form:select>
                            </td>
                            <td><form:errors class="error" path="occurenceFrom" cssClass="error"/></td>
                            </tr>
                            <tr>
                                <td class="outputInTable"><form:label path="occurenceTo"><fmt:message key="mushroom.occurenceTo"/></form:label></td>
                                    <td>
                                    <form:select path="occurenceTo">
                                <option value=""><fmt:message key="general.prompt"/></option>
                                <c:forEach var="month" items="${months}">
                                    <c:if test="${month == command.occurenceTo}">
                                        <option value="${month}" selected="selected"><fmt:message key="${month}"/></option>
                                    </c:if>
                                    <c:if test="${month != command.occurenceTo}">
                                        <option value="${month}"><fmt:message key="${month}"/></option>
                                    </c:if>
                                </c:forEach>
                            </form:select>
                            </td>
                            <td><form:errors class="error" path="occurenceTo" cssClass="error"/></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td>
                                    <input type="submit" value=<fmt:message key="general.submit"/> />
                                </td>
                                <td></td>
                            </tr>
                        </table>
                    </form:form>   
                </div>
                <p class="error">${error}</p>
                <br/>
                <div class="divOfItems">
                    <div id="title_subsection">
                        <h3><fmt:message key="mushroom.all"/></h3>
                    </div>
                    <table class="allTable">
                        <th><fmt:message key="mushroom.name"/></th>
                        <th><fmt:message key="mushroom.type"/></th>
                        <th><fmt:message key="mushroom.occurenceFrom"/></th>
                        <th><fmt:message key="mushroom.occurenceTo"/></th>
                        <sec:authorize access="hasRole('ROLE_ADMIN')">
                            <th colspan="2"><fmt:message key="general.action"/></th>
                        </sec:authorize>
                        <c:forEach var="mushroom" items="${mushrooms}" >
                            <tr>
                                <td><c:out value="${mushroom.name}" /></td>
                                <td><fmt:message key="${mushroom.type}"/></td>
                                <td><fmt:message key="${mushroom.occurenceFrom}"/></td>
                                <td><fmt:message key="${mushroom.occurenceTo}"/></td>
                                <sec:authorize access="hasRole('ROLE_ADMIN')">
                                    <td><a href="editMushroom?id=${mushroom.id}"><fmt:message key="general.edit"/></a></td>
                                    <td><a href="removeMushroom?id=${mushroom.id}" 
                                           onclick="return confirm('Are you sure you want to delete ${mushroom.name}?')">
                                            <fmt:message key="general.remove"/></a></td>    
                                        </sec:authorize>
                            </tr>
                        </c:forEach>
                    </table>
                    <fmt:message key="sortoptions"/><br />
                    <form action="mushroomSort" >
                        <fmt:message key="mushroom.type"/>:&nbsp;
                        <select name="sortBy">
                            <option value="EDIBLE"><fmt:message key="EDIBLE"/></option>
                            <option value="INEDIBLE"><fmt:message key="INEDIBLE"/></option>
                            <option value="POISONOUS"><fmt:message key="POISONOUS"/></option>
                            <option value="default"><fmt:message key="default"/></option>
                        </select>
                        <input type="submit" value="Submit" />
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
